package br.com.sgo.dao;



import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import br.com.caelum.vraptor.ioc.Component;
import br.com.sgo.model.Patient;

@Component
public class PatientDao {

	private final Session session;
	
	public PatientDao(Session session) {
		this.session = session;
	}
	
	public Patient load(Patient patient) {
		
		return (Patient) session.load(Patient.class, patient.getId());
	}
	
	public List<Patient> list() {
		
		return (List<Patient>) session.createCriteria(Patient.class).list();
	}
	
	public void save(Patient patient) {
		
		Transaction transaction = session.beginTransaction();
		try {
			session.merge(patient);
			transaction.commit();
		}
		catch(Exception e) {
			e.printStackTrace();
			transaction.rollback();
		}
		
	}
	
	public void delete(Patient patient) {
		
		Transaction transaction = session.beginTransaction();
		try {
			session.delete(patient);
			transaction.commit();
		}
		catch(Exception e) {
			e.printStackTrace();
			transaction.rollback();
		}
		
	}	
}
